package com.bianguo.android.baidumapdemo.activity;

import android.Manifest;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.media.MediaRecorder;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;

import com.bianguo.android.baidumapdemo.BaseActivity;
import com.bianguo.android.baidumapdemo.R;
import com.bianguo.android.baidumapdemo.service.RecordScreenService;
import com.bianguo.android.baidumapdemo.utils.CacheUtil;
import com.bianguo.android.baidumapdemo.utils.PermissionHelper;
import com.bianguo.android.baidumapdemo.utils.RecordScreenThread;

import java.io.File;

import butterknife.OnClick;

public class RecordScreenActivity extends BaseActivity {

    public static final int REQUEST_CODE_RECORD = 10003;
    private MediaProjectionManager mMediaProjectionManager;
    private MediaRecorder mediaRecorder;
    private boolean running;
    private RecordScreenService recordService;

    @Override
    protected int layoutId() {
        return R.layout.activity_record_screen;
    }

    @Override
    protected void initView(@Nullable Bundle saveInstanceState) {
        mMediaProjectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
        Intent intent = new Intent(this, RecordScreenService.class);
        bindService(intent, connection, BIND_AUTO_CREATE);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @OnClick({R.id.record_screen_btn})
    public void onClickBk(View view) {
        switch (view.getId()) {
            case R.id.record_screen_btn:
                boolean permission1 = PermissionHelper.getInstance(mContext).isPermission(mContext, new String[]{Manifest.permission.RECORD_AUDIO, Manifest.permission.WRITE_EXTERNAL_STORAGE}, PermissionHelper.PERMISSION_RECORD);
                if (permission1) {
                    Intent screenCaptureIntent = mMediaProjectionManager.createScreenCaptureIntent();
                    startActivityForResult(screenCaptureIntent, REQUEST_CODE_RECORD);
                }
                break;
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (REQUEST_CODE_RECORD == requestCode) {
            MediaProjection mediaProjection = mMediaProjectionManager.getMediaProjection(resultCode, data);
            File file = new File(CacheUtil.CACHE_DIR);
            if (!file.exists()) {
                file.mkdirs();
            }
            recordService.setMediaProject(mediaProjection);
            recordService.startRecord();
//            RecordScreenThread screenThread = new RecordScreenThread(1920, 1080, 1024, 68, mediaProjection, CacheUtil.CACHE_DIR + System.currentTimeMillis() + ".mp4");
//            screenThread.start();
        }
    }

    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName className, IBinder service) {
            DisplayMetrics metrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);
            RecordScreenService.RecordBinder binder = (RecordScreenService.RecordBinder) service;
            recordService = binder.getRecordService();
            recordService.setConfig(metrics.widthPixels, metrics.heightPixels, metrics.densityDpi);
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {}
    };
}
